package com.ibeeking.found.upms.b.rest.service.impl;

import com.ibeeking.found.upms.b.rest.service.IOpenUrlRelationService;
import com.ibeeking.found.upms.service.common.bo.OpenUrlRelationBO;
import com.ibeeking.found.upms.service.common.dos.OpenUrlRelationDO;
import com.ibeeking.found.upms.service.common.param.OpenUrlUserParam;
import com.ibeeking.found.upms.service.common.param.OpenUserUrlParam;
import com.ibeeking.found.upms.service.mapper.OpenUrlRelationMapper;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName OpenUrlRelationServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-04-01 11:11
 **/
@Service
public class OpenUrlRelationServiceImpl implements IOpenUrlRelationService {

    @Resource
    private OpenUrlRelationMapper openUrlRelationMapper;

    @Override
    public Boolean delByUserId(Long openUserId) {
        return openUrlRelationMapper.delete(new LambdaQueryWrapper<>(OpenUrlRelationDO.class).eq(OpenUrlRelationDO::getOpenId, openUserId)) > 0;
    }

    @Override
    public Boolean delByUrlId(Long openUrlId) {
        return openUrlRelationMapper.delete(new LambdaQueryWrapper<>(OpenUrlRelationDO.class).eq(OpenUrlRelationDO::getOpenUrlId, openUrlId)) > 0;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Boolean modifyByOpenUrlId(OpenUrlUserParam param) {
        openUrlRelationMapper.delete(new LambdaQueryWrapper<>(OpenUrlRelationDO.class).eq(OpenUrlRelationDO::getOpenUrlId, param.getOpenUrlId()));
        if (null == param.getOpenUserId() || 0 == param.getOpenUserId().length) {
            return true;
        }
        List<OpenUrlRelationDO> saves = new ArrayList<>();
        for (Long openUserId : param.getOpenUserId()) {
            OpenUrlRelationDO openUrlRelationDO = new OpenUrlRelationDO();
            openUrlRelationDO.setOpenId(openUserId);
            openUrlRelationDO.setOpenUrlId(param.getOpenUrlId());

            saves.add(openUrlRelationDO);
        }
        return openUrlRelationMapper.insertBatchSomeColumn(saves) > 0;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Boolean modifyByOpenUserId(OpenUserUrlParam param) {
        openUrlRelationMapper.delete(new LambdaQueryWrapper<>(OpenUrlRelationDO.class).eq(OpenUrlRelationDO::getOpenId, param.getOpenUserId()));
        if (null == param.getOpenUrlId() || 0 == param.getOpenUrlId().length) {
            return true;
        }
        List<OpenUrlRelationDO> saves = new ArrayList<>();
        for (Long openUrlId : param.getOpenUrlId()) {
            OpenUrlRelationDO openUrlRelationDO = new OpenUrlRelationDO();
            openUrlRelationDO.setOpenId(param.getOpenUserId());
            openUrlRelationDO.setOpenUrlId(openUrlId);
            saves.add(openUrlRelationDO);
        }
        return openUrlRelationMapper.insertBatchSomeColumn(saves) > 0;
    }

    @Override
    public List<OpenUrlRelationBO> queryByUrlId(Long openUrlId) {
        List<OpenUrlRelationDO> openUrlRelationDos = openUrlRelationMapper.selectList(new LambdaQueryWrapper<>(OpenUrlRelationDO.class).eq(OpenUrlRelationDO::getOpenUrlId, openUrlId));
        return JsonUtils.jsonToList(openUrlRelationDos, OpenUrlRelationBO.class);
    }

    @Override
    public List<OpenUrlRelationBO> queryByUserId(Long openUserId) {
        List<OpenUrlRelationDO> openUrlRelationDos = openUrlRelationMapper.selectList(new LambdaQueryWrapper<>(OpenUrlRelationDO.class).eq(OpenUrlRelationDO::getOpenId, openUserId));
        return JsonUtils.jsonToList(openUrlRelationDos, OpenUrlRelationBO.class);
    }
}
